package com.dlwx.parttimetalent.utils;

import android.content.Context;

import com.dlwx.parttimetalent.bean.ApiTokenBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;

/**
 * Created by Administrator on 2017/11/28.
 */

public class OkgoUtil {

    private static String apiToken;
    private static int clientId;
    private static int userId;
    private static String userToken;

    public static void getApiToken(final Context context, String id){
        OkGo.<String>get(Http.API_TOKEN)
                .params("deviceNo",id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if(response==null){ return; }
                            String res=response.body().toString();
                            ApiTokenBean bean= new Gson().fromJson(res, ApiTokenBean.class);
                            if(bean.getCode()== Constant.SUCESS){
                                ApiTokenBean.BodyBean bodyBean=bean.getBody();
                                if(bodyBean==null){return;}

                                SPUtils.put(context,Constant.API_TOKEN,bodyBean.getApitoken());
                                SPUtils.put(context,Constant.CLIENT_ID,bodyBean.getClientid());
                            }
                    }
                });
    }

    public static   GetRequest<String> get(String url, Context context){
        getBasicInfo(context);
        GetRequest<String> getRequest = OkGo.<String>get(url);
        getRequest
                .params("apitoken",apiToken)
                .params("clientid",clientId)
                .params("userid",userId)
                .params("user_token",userToken);
        return getRequest;
    }

    public static PostRequest<String> post(String url, Context context){
        getBasicInfo(context);
        PostRequest<String> postRequest = OkGo.<String>post(url+"?apitoken="+apiToken+"&clientid="+clientId+"&userid="+userId+"&user_token="+userToken);
        return postRequest;
    }
    public static PostRequest<String> postNoUser(String url, Context context){
        getBasicInfo(context);
        PostRequest<String> postRequest = OkGo.<String>post(url+"?apitoken="+apiToken+"&clientid="+clientId);
        return postRequest;
    }

    private static void getBasicInfo(Context context){
//        if(TextUtils.isEmpty(apiToken)){
//        }
//        if(TextUtils.isEmpty(userToken)){
//        }
        apiToken = (String) SPUtils.get(context, Constant.API_TOKEN, "");
        clientId = (int) SPUtils.get(context, Constant.CLIENT_ID, 0);
        userId=(int) SPUtils.get(context, Constant.USER_ID, 0);
        userToken=(String) SPUtils.get(context, Constant.USER_Token, "");
    }
}
